<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<head>
<title>Web Console</title>
</head>

<script type="text/javascript" src="js/prototype-1.7.1.js"></script>

<link rel="stylesheet" href="js/ansi.css" type="text/css" media="all" />
<script
	src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"
	type="text/javascript" charset="utf-8"></script>
<script src="js/jqconsole.min.js"></script>

<style>
html,body {
	background-color: #333;
	color: white;
	font-family: monospace;
	margin: 0;
	padding: 0;
}

#console {
	height: 500px;
	width: 1000px;
	position: relative;
	background-color: black;
	border: 2px solid #CCC;
	margin: 0 auto;
	margin-top: 50px;
}

.jqconsole {
	padding: 10px;
	padding-bottom: 10px;
}

.jqconsole-cursor {
	background-color: #999;
}

.jqconsole-blurred .jqconsole-cursor {
	background-color: #666;
}

.jqconsole-prompt {
	color: #0d0;
}

.jqconsole-old-prompt {
	color: #0b0;
	font-weight: normal;
}

.jqconsole-input {
	color: #dd0;
}

.jqconsole-old-input {
	color: #bb0;
	font-weight: normal;
}

.brace {
	color: #00FFFF;
}

.paran {
	color: #FF00FF;
}

.bracket {
	color: #FFFF00;
}

.jqconsole-composition {
	background-color: red;
}
</style>

<script type="text/javascript">
	jQuery.noConflict();
	var li = -1;
	var promptInited = false;
	var lastText = "";
	var DEFAULT_UPDATE_FREQUENCY = 5;

	function appendLog(records) {

		for ( var i = 0; i < records.length; i += 1) {
			jqconsole.Write(records[i].msg + "\n");
			li = records[i].index;
		}
	}

	function receiveLogAsync(pe) {
		new Ajax.Request('/ws/server/log', {
			method : 'get',
			parameters : {
				"li" : li
			},
			requestHeaders : {
				Accept : 'application/json'
			},
			onSuccess : function(transport) {
				callback = eval("(function(){return " + transport.responseText + ";})()");
				appendLog(callback.records);
				updateConsole(callback.consoleBusy);
			},
			onFailure : function() {
				alert('Unable to receive responce from the server');
			}
		});
	}

	function updaterStart() {
		if (!window.updater) {
			receiveLogAsync();
			window.updater = new PeriodicalExecuter(function(pe) {
				receiveLogAsync(pe);
			}, DEFAULT_UPDATE_FREQUENCY);
		}
	}
	
	function updaterStop() {
		if (window.updater) {
			receiveLogAsync();
			window.updater.stop();
			window.updater = undefined;
		}
	}

	function submitInput(command) {
		jqconsole.Disable();
		new Ajax.Request(
				'/ws/server/command',
				{
					method : 'post',
					parameters : {
						"cmd" : command,
						"li" : li
					},
					onSuccess : function(transport) {
						callback = eval("(function(){return " + transport.responseText + ";})()");
						appendLog(callback.records);
						updateConsole(callback.consoleBusy);
					},
					onFailure : function() {
						jqconsole.Write('ERROR: Unable to receive responce from the server\n');
					}
				});

	}

	function updateConsole(busy) {
		if (!promptInited)
			handler();

		if (!busy && jqconsole.IsDisabled()) {
			jqconsole.Enable();
			handler();
		}

		if (busy && !jqconsole.IsDisabled()) {
			jqconsole.Disable();
			updaterStart();
		}
		
		if (!busy) {
			updaterStop();
		} else {
			updaterStart();
		}
	}
</script>

<script>
	jQuery(function() {
		// Creating the console.
		window.jqconsole = jQuery('#console').jqconsole("", '> ');

		// Abort prompt on Ctrl+Z.
		jqconsole.RegisterShortcut('Z', function() {
			jqconsole.AbortPrompt();
			handler();
		});

		// Move to line start Ctrl+A.
		jqconsole.RegisterShortcut('A', function() {
			jqconsole.MoveToStart();
			handler();
		});

		// Move to line end Ctrl+E.
		jqconsole.RegisterShortcut('E', function() {
			jqconsole.MoveToEnd();
			handler();
		});

		jqconsole.RegisterMatching('{', '}', 'brace');
		jqconsole.RegisterMatching('(', ')', 'paran');
		jqconsole.RegisterMatching('[', ']', 'bracket');
		// Handle a command.
		handler = function(command) {
			if (command) {
				try {
					submitInput(command);
				} catch (e) {
					jqconsole.Write('ERROR: ' + e.message + '\n');
				}
			} else {
				promptInited = true;
				jqconsole.Prompt(true, handler, function(command) {
					return false;
				});
			}
		};

		updaterStart();
	});
</script>

<body>
	<div id="console"></div>
</body>
</html>